package activity.luntan;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import adapter.TieZiMeAdapter;
import base.BaseActivity;
import base.MyApplcation;
import bean.TopicListlBean;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import presenter.LunTanPresenter;
import util.utls.BasePtr;
import util.utls.ByAlert;
import util.utls.Const;
import util.utls.SPUtils;
import util.volley.ResultEntity;

/**
 * 我的帖子
 */
public class TieZiMeActivity extends BaseActivity implements Observer {
    TextView tvTitle;
    ImageView back;
    ImageView img_other;
    TextView kong_text;
    PtrFrameLayout commend_anchor_ptr;
    LinearLayout kong_layoout;
    ListView main_listview;
    String uid;
    LunTanPresenter presenter;
    TieZiMeAdapter adapter;
    TopicListlBean bean;
    List<TopicListlBean.TopicList> datas;
    int size = 1;
    int page = 10;
    int deleteId;//删除的是哪一条

    private Dialog dialog;// 自定义Dialog
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tie_zi_me);
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);
        tvTitle.setText("我的帖子");

        img_other.setVisibility(View.VISIBLE);
        img_other.setImageResource(R.drawable.xiao);
        uid = (String) SPUtils.get(MyApplcation.ctx, null, Const.ID, "");
        bean=new TopicListlBean();
        datas=new ArrayList<>();
        presenter=new LunTanPresenter(this);
        showExtraDialog();
        showProgressDialog("正在加载,请稍后...",true);
        presenter.myTieZi(uid,size,page);

        adapter=new TieZiMeAdapter(TieZiMeActivity.this,datas);
        main_listview.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                size++;
                presenter.myTieZi(uid,size,page);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.myTieZi(uid,size,page);
            }
        });
        commend_anchor_ptr.autoRefresh(true);

        intList();
    }

    /**
     * 操作list
     */
    private void intList(){
        main_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TieZiMeActivity.this, ToPicDetailActivity.class);
                intent.putExtra("id", datas.get(position).getId());
                startActivity(intent);
            }
        });
        main_listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                deleteId = datas.get(position).getId();
                dialog.show();
                return true;
            }
        });

    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.img_other:
                Intent intent=new Intent(TieZiMeActivity.this,TieZiMessageActivity.class);
                startActivity(intent);

                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (!entity.getCode().equals("0")) {
                ByAlert.alert(entity.getMsg());
                return;
            }
            //我的帖子
            if (entity.getEventType() == LunTanPresenter.my_tiezi_success) {
                commend_anchor_ptr.refreshComplete();
                if (size == 1) {
                    datas.clear();
                }
                bean = (TopicListlBean) entity.getData();
                if (bean != null) {
                    datas.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                    if (datas.size()>0){
                        kong_layoout.setVisibility(View.GONE);
                        main_listview.setVisibility(View.VISIBLE);
                        commend_anchor_ptr.setVisibility(View.VISIBLE);
                    }else{
                        kong_layoout.setVisibility(View.VISIBLE);
                        main_listview.setVisibility(View.GONE);
                        commend_anchor_ptr.setVisibility(View.GONE);
                        kong_text.setText("暂时没有帖子");
                    }
                }

            } else if (entity.getEventType() == LunTanPresenter.my_tiezi_fail) {
                ByAlert.alert(entity.getData());
            }
            else if (entity.getEventType() == LunTanPresenter.delete_tiezi_success) {
                ByAlert.alert(entity.getData());

                Iterator<TopicListlBean.TopicList> iterator=datas.iterator();
                while (iterator.hasNext()){
                    TopicListlBean.TopicList bList=iterator.next();
                    if (deleteId==bList.getId()){
                        iterator.remove();
                        adapter.notifyDataSetChanged();

                        if (datas.size()>0){
                            kong_layoout.setVisibility(View.GONE);
                            main_listview.setVisibility(View.VISIBLE);

                        }else{
                            kong_layoout.setVisibility(View.VISIBLE);
                            main_listview.setVisibility(View.GONE);
                            kong_text.setText("暂时没有帖子");
                        }
                        break;
                    }
                }
            }else if (entity.getEventType() == LunTanPresenter.delete_tiezi_fail ){
                ByAlert.alert(entity.getData());
            }
        }
    }

    /**
     * 删除Dialog
     */
    private void showExtraDialog() {
        dialog = new Dialog(this, R.style.rel_dialog);
        View view = LayoutInflater.from(this).inflate(
                R.layout.dialog_delete, null);
        dialog.setContentView(view);

        TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
        TextView dialog_quxiao = (TextView) view.findViewById(R.id.dialog_quxiao);
        TextView user_title = (TextView) view.findViewById(R.id.user_title);
        TextView dialog_conext = (TextView) view.findViewById(R.id.dialog_conext);
        user_title.setText("删除帖子");
        dialog_conext.setText("确定删除该帖子？");

        dialog.setCanceledOnTouchOutside(false);
        dialog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                showProgressDialog("删除中,请稍后", true);
                presenter.deleteTieZi(uid,deleteId+"");
            }
        });
        dialog_quxiao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        int screenWidth = display.getWidth();
        // 屏幕高度
        int screenHeight = display.getHeight();
        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        lp.width = screenWidth * 3 / 4;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }
}
